package com.lzx.iteam.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.lzx.iteam.R;

import java.util.Calendar;
import java.util.TimeZone;

/**
 * Created by Megan on 16/3/29.
 */
public class MyClockView extends View {
    private static String TAG = "AbAnalogClock";
    private Drawable mHourHand;
    private Drawable mMinuteHand;
    private Drawable mSecondHand;
    private Drawable mDial;
    private Calendar mCalendar;
    public static final int NEED_INVALIDATE = 0X23;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NEED_INVALIDATE:
                    mCalendar = Calendar.getInstance();
                    invalidate();//告诉UI主线程重新绘制
                    handler.sendEmptyMessageDelayed(NEED_INVALIDATE, 1000);
                    break;
                default:
                    break;
            }

        }
    };

    public MyClockView(Context context, AttributeSet attrs) {
        super(context);
        mDial = this.getContext().getResources().getDrawable(R.drawable.clock_dial);
        mHourHand = this.getContext().getResources().getDrawable(R.drawable.clock_hour);
        mMinuteHand = this.getContext().getResources().getDrawable(R.drawable.clock_minute);
        mSecondHand = this.getContext().getResources().getDrawable(R.drawable.clock_second);
        mCalendar = Calendar.getInstance();
        handler.sendEmptyMessage(NEED_INVALIDATE);//向handler发送一个消息，让它开启重绘
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int minute = mCalendar.get(Calendar.MINUTE);//得到当前分钟数
        int hour = mCalendar.get(Calendar.HOUR);//得到当前小时数
        int sec = mCalendar.get(Calendar.SECOND);//得到当前秒数

        int availableWidth = this.getRight() - this.getLeft();
        int availableHeight = this.getBottom() - this.getTop();
        int x = availableWidth / 2;
        int y = availableHeight / 2;
        Drawable dial = this.mDial;
        int w = dial.getIntrinsicWidth();
        int h = dial.getIntrinsicHeight();

        // TODO 时钟背景图
        if (availableWidth < w || availableHeight < h) {
            float hourHand = Math.min((float) availableWidth / (float) w, (float) availableHeight / (float) h);
            canvas.save();
            canvas.scale(hourHand, hourHand, (float) x, (float) y);
        }
        dial.setBounds(x - w / 2, y - h / 2, x + w / 2, y + h / 2);
        dial.draw(canvas);

        // TODO 时针
        canvas.save();
        canvas.rotate((hour * 60.0F + minute) /12 /60 * 360.0F, (float) x, (float) y);
        Drawable hourHand1 = this.mHourHand;
        w = hourHand1.getIntrinsicWidth();
        h = hourHand1.getIntrinsicHeight();
        hourHand1.setBounds(x - w / 2, y - h / 2, x + w / 2, y + h / 2);
        hourHand1.draw(canvas);
        canvas.restore();

        // TODO 分针
        canvas.save();
        canvas.rotate(minute / 60.0F * 360.0F, (float) x, (float) y);
        Drawable minuteHand = this.mMinuteHand;
        w = minuteHand.getIntrinsicWidth();
        h = minuteHand.getIntrinsicHeight();
        minuteHand.setBounds(x - w / 2, y - h / 2, x + w / 2, y + h / 2);
        minuteHand.draw(canvas);
        canvas.restore();

        // TODO 秒针
        canvas.save();
        canvas.rotate(sec / 60.0F * 360.0F, (float) x, (float) y);
        Drawable secondHand = this.mSecondHand;
        w = secondHand.getIntrinsicWidth();
        h = secondHand.getIntrinsicHeight();
        secondHand.setBounds(x - w / 2, y - h / 2, x + w / 2, y + h / 2);
        secondHand.draw(canvas);
        canvas.restore();

    }
}